package com.ft.mybatis.druid;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.alibaba.druid.wall.WallConfig;
import com.alibaba.druid.wall.WallFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import javax.servlet.Filter;
import javax.servlet.Servlet;

/**
 * @copyright (C), 2015-2019, XXX有限公司
 * @fileName: DruidConfiguration
 * @author: 李阳
 * @date: 2019/5/13 11:46
 * @description: Druid配置类
 * @history: <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
@Configuration
@Slf4j
public class DruidConfiguration implements EnvironmentAware {

    private Environment env;

    @Override
    public void setEnvironment(Environment environment) {
        this.env = environment;
    }

    @Bean
    public ServletRegistrationBean druidServletRegistrationBean() {
        log.info("Druid监控配置...");
        ServletRegistrationBean<Servlet> servletRegistrationBean = new ServletRegistrationBean<>(new StatViewServlet(), env.getProperty("spring.datasource.druid.stat-view-servlet.url-pattern","/druid/*"));
        //白名单
        servletRegistrationBean.addInitParameter("allow", env.getProperty("spring.datasource.druid.stat-view-servlet.allow","127.0.0.1"));
        //黑名单
        servletRegistrationBean.addInitParameter("deny",env.getProperty("spring.datasource.druid.stat-view-servlet.deny",""));
        //超级管理员 用户名 密码
        servletRegistrationBean.addInitParameter("loginUsername", env.getProperty("spring.datasource.druid.stat-view-servlet.login-username","admin"));
        servletRegistrationBean.addInitParameter("loginPassword", env.getProperty("spring.datasource.druid.stat-view-servlet.login-password","nicaiwoshishui"));
        //是否能够重置数据  禁用HTML的 reset all 功能
        servletRegistrationBean.addInitParameter("resetEnable",env.getProperty("spring.datasource.druid.stat-view-servlet.reset-enable","false"));
        return servletRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean(){
        log.info("Druid过滤配置...");
        FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<>(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns(env.getProperty("spring.datasource.druid.web-stat-filter.url-pattern","/*"));
        filterRegistrationBean.addInitParameter("exclusions",env.getProperty("spring.datasource.druid.web-stat-filter.exclusions","/*"));
        return filterRegistrationBean;
    }

    @Bean
    public WallFilter wallFilter() {
        log.info("Druid防火墙过滤配置...");
        WallFilter wallFilter = new WallFilter();
        wallFilter.setConfig(wallConfig());
        return wallFilter;
    }

    /**
     * url: jdbc:mysql://xx.xx.xx.xx:3306/kbms?useSSL=false&allowMultiQueries=true
     * @return
     */
    @Bean
    public WallConfig wallConfig() {
        log.info("Druid防火墙批处理语句开启配置...");
        WallConfig config = new WallConfig();
        //允许一次执行多条语句
        config.setMultiStatementAllow(true);
        //允许一次执行多条语句
        config.setNoneBaseStatementAllow(true);
        return config;
    }

}
